<template>
  <div class="user">
    <div class="search">
      <page-search
        :searchConfig="searchConfig"
        @query-btn-click="handleQueryClick"
        @reset-btn-click="handleResetClick"
      ></page-search>
    </div>

    <page-content
      ref="pageContentRef"
      :contentTableConfig="contentTableConfig"
      page-name="Users"
      @edit-btn-click="handelEditData"
      @new-btn-click="handelNewData"
    ></page-content>

    <page-model
      :defaultInfo="defaultInfo"
      :model-config="modalConfigRef"
      ref="pageModelRef"
      title="新建用户"
      page-name="Users"
    ></page-model>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, computed } from 'vue'
import PageContent from '@/components/page-content'
import searchConfig from './config/search'
import pageSearch from '@/components/page-search'

import { contentTableConfig } from './config/content.config'
import { usePageSearch } from '@/hooks/use-page-search'

import modelConfig from './config/model.config'
import pageModel from '@/components/page-model'
import { usePageModel } from '@/hooks/use-page-model'
import { useStore } from '@/store'
export default defineComponent({
  name: 'user',
  components: {
    PageContent,
    pageSearch,
    pageModel
  },
  setup() {
    //
    const store = useStore()
    const modalConfigRef = computed(() => {
      //获得部门角色
      const departmentItem = modelConfig.formItems.find((item) => {
        return item.field === 'departmentId'
      })
      departmentItem!.optinos = store.state.entireDepartment.map((item) => {
        return { title: item.name, value: item.id }
      })
      const roleItem = modelConfig.formItems.find((item) => {
        return item.field === 'roleId'
      })
      roleItem!.optinos = store.state.entireRole.map((item) => {
        return { title: item.name, value: item.id }
      })
      return modelConfig
    })
    const newCallBack = () => {
      const passwordItem = modelConfig.formItems.find(
        (item) => item.field === 'password'
      )
      passwordItem!.isHidden = false
    }
    const editCallBack = () => {
      const passwordItem = modelConfig.formItems.find(
        (item) => item.field === 'password'
      )
      passwordItem!.isHidden = true
    }

    const { pageModelRef, handelEditData, handelNewData, defaultInfo } =
      usePageModel(newCallBack, editCallBack)
    // const pageContentRef = ref<InstanceType<typeof PageContent>>()
    // const handleResetClick = (): void => {
    //   pageContentRef.value?.getPageData()
    // }
    // const handleQueryClick = (queryInfo: any): void => {
    //   pageContentRef.value?.getPageData(queryInfo)
    // }

    return {
      pageModelRef,
      defaultInfo,
      handelEditData,
      handelNewData,
      searchConfig,
      contentTableConfig,

      modalConfigRef,
      ...usePageSearch()
      // handleResetClick,
      // handleQueryClick,
      // pageContentRef
    }
  }
})
</script>

<style scoped lang="less">
.content {
}
.handle-btn {
  display: flex;
  text-align: center;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  .el-button {
    margin-left: 0;
  }
}
</style>
